package com.itheima.mall.web.sms;



import com.itheima.mall.pojo.sms.Coupon;
import com.itheima.mall.service.sms.CouponService;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import com.itheima.mall.vo.sms.CouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;
    //添加优惠券
    @PostMapping("create")
    public R addCoupon(@RequestBody CouponVo couponVo){
        couponService.addCoupon(couponVo);
        return R.succeed(null, "添加成功");
    }

    //删除优惠券
    @PostMapping("delete/{id}")
    public R deleteCoupon(@PathVariable(value = "id")Integer id){
        couponService.deleteCoupon(id);
        return R.succeed(null, "删除成功");
    }

    //根据优惠券名称和类型分页获取优惠券列表
    @GetMapping("list")
    public R getCoupon( String name,Integer pageNum,Integer pageSize,Integer type){
        PageResult<Coupon> coupon=couponService.getCoupon(name,pageNum,pageSize,type);
        return R.succeed(coupon, "分页成功");
    }



    //修改优惠券
    @PostMapping("update/{id}")
    public R upadateCoupon(@RequestBody(required = false) CouponVo couponVo,@PathVariable("id") Integer id){
        couponService.upadateCoupon(couponVo,id);
        return R.succeed(null, "修改成功");
    }

    //获取单个优惠券的详细信息
    @GetMapping("{id}")
    public R selectCoupon(@PathVariable("id") Integer id){
        CouponVo vo=couponService.selectCoupon(id);
        return R.succeed(vo, "查询成功");
    }
}
